package com.yex.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yex.common.utils.PageUtils;
import com.yex.dao.NoticeUserDao;
import com.yex.entity.NoticeUserEntity;
import com.yex.service.LocaleMessageSourceService;
import com.yex.service.NoticeUserService;
import com.yex.utils.JsonResultVo;
import com.yex.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;


@Service("noticeUserService")
public class NoticeUserServiceImpl extends ServiceImpl<NoticeUserDao, NoticeUserEntity> implements NoticeUserService {
    @Autowired
    private LocaleMessageSourceService message;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<NoticeUserEntity> page = this.selectPage(
                new Query<NoticeUserEntity>(params).getPage(),
                new EntityWrapper<NoticeUserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public JsonResultVo insertNoticeUser(String noticeId, Integer userId) {
        NoticeUserEntity noticeUser = baseMapper.selectNoticeUser(noticeId, userId);
        if (noticeUser != null) {
            return new JsonResultVo(message.getMessage("fail"), -1);
        }
        NoticeUserEntity noticeUserEntity = new NoticeUserEntity();
        noticeUserEntity.setUserId(userId);
        noticeUserEntity.setNoticeId(Integer.valueOf(noticeId));
        noticeUserEntity.setCreateTime(new Date());
        Integer insert = baseMapper.insert(noticeUserEntity);
        return insert > 0 ? new JsonResultVo(message.getMessage("success")) : new JsonResultVo(message.getMessage("fail"), -1);
    }

    @Override
    public NoticeUserEntity selectNoticeUser(String noticeId, Integer userId) {
        return baseMapper.selectNoticeUser(noticeId, userId);
    }


}
